/*
 *所有关于Demo_Product类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Demo_ProductService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Demo.IRepositories;
using System.Collections.Generic;
using VOL.Core.WorkFlow;

namespace VOL.Demo.Services
{
    public partial class Demo_ProductService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IDemo_ProductRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public Demo_ProductService(
            IDemo_ProductRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            //AddWorkFlowExecuting = (Demo_Product product) =>
            //{
            //    if (product.Price<100)
            //    {
            //        return false;
            //    }
            //    //返回false，当前数据不会进入审批流程
            //    return true;
            //};

            //新建的数据写入审批流程后,第二个参数为审批人的用户id
            AddWorkFlowExecuted = (Demo_Product order, List<int> userIds) =>
            {
                //这里可以做发邮件通知
                //var userInfo = repository.DbContext.Set<Sys_User>()
                //                .Where(x => userIds.Contains(x.User_Id))
                //                .Select(s => new { s.User_Id, s.UserTrueName, s.Email, s.PhoneNo }).ToList();


                //var email = userInfo.Select(s => s.Email).ToArray();

                //MailHelper.Send("测试", "邮件内容", email);
            };
            return base.Add(saveDataModel);
        }


        public override WebResponseContent Audit(object[] keys, int? auditStatus, string auditReason)
        {
            WebResponseContent webResponse = new WebResponseContent();
            //status当前审批状态,lastAudit是否最后一个审批节点
            AuditWorkFlowExecuting = (Demo_Product order, AuditStatus status, bool lastAudit) =>
            {
                return webResponse.OK();
            };
            //status当前审批状态,nextUserIds下一个节点审批人的帐号id(可以从sys_user表中查询用户具体信息),lastAudit是否最后一个审批节点
            AuditWorkFlowExecuted = (Demo_Product order, AuditStatus status, List<int> nextUserIds, bool lastAudit) =>
            {
                //lastAudit=true时，流程已经结束
                if (!lastAudit)
                {
                    //这里可以给下一批审批发送邮件通知
                    //var userInfo = repository.DbContext.Set<Sys_User>()
                    //             .Where(x => nextUserIds.Contains(x.User_Id))
                    //             .Select(s => new { s.User_Id, s.UserTrueName, s.Email, s.PhoneNo }).ToList();

                    //var email = userInfo.Select(s => s.Email).ToArray();

                    //MailHelper.Send("测试", "邮件内容", email);

                    // order.CreateID为当前数据提交人
                }

                return webResponse.OK();
            };

            return base.Audit(keys, auditStatus, auditReason);
        }

    }
}
